package client;

import java.io.DataInputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.Vector;
import java.io.DataOutputStream;

import javax.swing.JOptionPane;

import UIGame.Giaodien;

import libarary.Contants;
import libarary.Contants.state;

/**
 * 
 * @author ADMIN
 */
public class Client extends Thread implements Contants {

	/** Creates a new instance of Client */
	/**/
	Socket socket;
	DataInputStream din;
	DataOutputStream dos;
	String addIp;
	public static String nameuser;
	int port;
	Giaodien obj;
	public static boolean loop = true;
	public static String receiveData = "receive", sendData = "send";

	public Client(String addIp, int port) {
		this.addIp = addIp;
		this.port = port;
		obj = new Giaodien(this);
		obj.getjfmain().pack();
		obj.getjfmain().setResizable(false);
		obj.getjfmain().setVisible(true);
		try {
			if (socket == null) {
				socket = new Socket(addIp, port);
				dos = new DataOutputStream(socket.getOutputStream());
				din = new DataInputStream(socket.getInputStream());
			}
			this.start();
		} catch (IOException ex) {
			ex.printStackTrace();
		}
	}

	public int checkReceiveData(String st) {
		if (st.equals("") != true) {
			char tv = st.charAt(0);
			switch (tv) {
			case '0':
				return state.Login;
			case '1':
				return state.danhsach;
			case '2':
				return state.thoat;
			case '3':
				return state.update;
			case '4':
				return state.chat;
			case '5':
				return state.updatechat;
			case '7':
				return state.playgame;
			case '8':
				return state.gameover;
			case '9':
				return state.gamescore;
			case 't'://update time
				return 10;
			case 'o'://update time
				return 11;
			
			}
		}
		return -1;
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		super.run();
		while (loop) {
			try {
				if (socket.isClosed() != true) {
					receiveData = din.readUTF().toString();
					int i = checkReceiveData(receiveData);
					receiveData = receiveData.substring(1);
					//System.out.println(receiveData);
					switch (i) {
					case state.Login:
						this.nameuser=receiveData.substring(0,receiveData.indexOf(state.noichuoi));
						receiveData=receiveData.substring(receiveData.indexOf(state.noichuoi));
						obj.Login(receiveData);
						break;
					case state.update:
						obj.updateUserlist(receiveData);
						break;
					case state.updatechat:
						obj.updateRoomChat(receiveData);
						break;
					case state.playgame:
						obj.iug.iuxg.game.handleButtonPressed();
						obj.iug.iuxg.game.component.requestFocus();
						break;
					case state.gameover:
						obj.iug.iuxg.game.handleGameOver();
						
						//
						//obj.iug.iug.game.component.requestFocus();
						break;
					case state.gamescore:
						obj.iug.iuxg.setvchat(chekDanhsah(receiveData));
						obj.iug.iuxg.jsplitchatxg.setRightComponent(obj.iug.iuxg.Uichat());
						break;
					case 10:
						receiveData = receiveData.substring(state.noichuoi.length());
						String mini=receiveData.substring(0,receiveData.indexOf(state.noichuoi));
						receiveData = receiveData.substring(receiveData.indexOf(state.noichuoi));
						String sec=receiveData.substring(state.noichuoi.length());
						if(sec.equals("0"))
							sec="00";
						obj.iug.iuxg.time=mini+":"+sec;
						obj.iug.iuxg.jsplitchatxg.setTopComponent(obj.iug.iuxg.panelTime());
						break;
					case 11:
						JOptionPane
						.showMessageDialog(null,
								"Score "+receiveData);
						sendData(state.gamescore+"");
						break;
					}

				} else
					loop = false;

			} catch (IOException e) {
				// TODO Auto-generated catch block
				loop = false;
				System.out.println("da thoat");
				e.printStackTrace();
			}
		}
	}

	// --
	public void sendData(String st) {
		try {

			dos.writeUTF(st);
			dos.flush();
		} catch (IOException ex) {
			ex.printStackTrace();
		}
	}

	public void close() {
		try {
			socket.close();
			// din.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	// ---
	public String receiveData() {
		try {

			return din.readUTF().trim();
		} catch (IOException ex) {
			ex.printStackTrace();
		}
		return null;
	}

	//
	public String getAddIp() {
		return addIp;
	}

	public int getPort() {
		return port;
	}

	//
	public static Vector chekDanhsah(String ds) {
		Vector v = new Vector(2, 3);
		String st = "";
		while (ds.equals(state.noichuoi) != true) {
				ds = ds.substring(state.noichuoi.length());
			v.add(ds.substring(0, ds.indexOf(state.noichuoi)));
			ds = ds.substring(ds.indexOf(state.noichuoi));
			

		}
		return v;
	}
	//public static Vector checkDiem(String ds){
		//Vector v = new Vector(2, 3);
		//String tam = "";
		//while (ds.equals(state.noichuoi) != true) {
	//}
	public static void main(String[] arg) {
		Client cl = new Client("localhost", 7000);

	}

}
